home *** CD-ROM | disk | FTP | other *** search
/ MacTech 1 to 12 / MacTech-vol-1-12.toast / Source / MacTech® Magazine / Volume 07 - 1991 / 07.08 Aug 91 / Dots Source / cScorePane.c < prev    next >
Encoding:
C/C++ Source or Header  |  1989-11-02  |  3.7 KB  |  171 lines  |  [TEXT/KAHL]

  1. /**************************************
  2.     cScorePane.c
  3.  
  4.     SUPERCLASS = CPane
  5.  
  6. Methods for the score pane. The score pane displays the current player
  7. turn and calculates and displays the game score.
  8.  
  9. ****************************************/
  10.  
  11. #include "cDotsDoc.h"
  12. #include "cScorePane.h"
  13. #include "dotsTypes.h"
  14. #include <Global.h>
  15.  
  16. /*** Class Constants ***/
  17. #define kScorePlace        30
  18. #define kHalfSpacing    (kScorePlace / 2)
  19. #define kScoreSpacing    (3*kScorePlace)
  20. #define kScoreInset        6
  21.  
  22. /*** Globals ***/
  23. extern    Pattern    *dgPlayerPats;
  24.  
  25. /******** C O N S T R U C T I O N ********/
  26.  
  27. /*** IScorePane
  28. *
  29. * Initialize instance variables
  30. */
  31. void cScorePane::IScorePane(CView *anEnclosure, CBureaucrat *aSupervisor,
  32.                             short aWidth, short aHeight,
  33.                             short aHEncl, short aVEncl,
  34.                             SizingOption aHSizing, SizingOption aVSizing)
  35. {
  36.         /* Highlighted score will be that of the player to move next */
  37.     fPlayerHilite = ((cDotsDoc *)aSupervisor)->getPlayerTurn();
  38.     
  39.         /* Call inherited method */
  40.     IPane(anEnclosure, aSupervisor, aWidth, aHeight,
  41.             aHEncl, aVEncl, aHSizing, aVSizing);
  42. }
  43.  
  44. /******** D R A W I N G **********/
  45.  
  46. /*** Draw {OVERRIDE}
  47. *
  48. * The area parameter gives the portion of the 
  49. * pane that needs to be redrawn. Area is in frame coordinates.
  50. */
  51. void cScorePane::Draw(Rect *area)
  52. {
  53.     tScores    score;
  54.     Boolean    player;
  55.     Rect    r;
  56.     Str255    s;
  57.     int        offset;
  58.     
  59.     TextFont(monaco);
  60.     TextFace(NULL);
  61.     TextSize(9);
  62.     PenNormal();
  63.     
  64.         /* Draw line across bottom of pane */
  65.     GetFrame(&r);
  66.     MoveTo(r.left, r.bottom-1);
  67.     LineTo(r.right, r.bottom-1);
  68.     
  69.     PenSize(kScoreInset, kScoreInset);    /* For frame around scores */
  70.     
  71.     calculateScore(score);
  72.     
  73.     for (player = kPlayer1; player <= kPlayer2; player++) {
  74.         NumToString(score[player], &s);
  75.         
  76.             /* Center score in player's rectangle */
  77.         getPlayerRect(player, &r);
  78.         offset = (r.right - r.left - StringWidth(s)) / 2;
  79.         MoveTo(r.left + offset, r.bottom - 3);
  80.         DrawString(s);
  81.         InsetRect(&r, -kScoreInset, -kScoreInset);
  82.         PenPat(&dgPlayerPats[player]);
  83.         FrameRect(&r);
  84.     }
  85.         /* Hilight/Unhighlight player score */
  86.     getPlayerRect(fPlayerHilite, &r);
  87.     InvertRect(&r);
  88. }
  89.  
  90.  
  91. /*** calculateScore
  92. *
  93. * Computes the current score
  94. */
  95. void cScorePane::calculateScore(tScores theScore)
  96. {
  97.     tBoxState    boxState;
  98.     int            row, col;
  99.     
  100.     theScore[kPlayer1] = 0;
  101.     theScore[kPlayer2] = 0;
  102.     
  103.         /* Check box states for entire matrix */
  104.     for (row = 0; row < kMaxRow; row++) {
  105.         for (col = 0; col < kMaxCol; col++) {
  106.             boxState = ((cDotsDoc *)itsSupervisor)->getBoxState(row, col);
  107.             switch (boxState) {
  108.                 case kBoxPlayer1:
  109.                     theScore[kPlayer1] = theScore[kPlayer1] + 1;
  110.                     break;
  111.                 case kBoxPlayer2:
  112.                     theScore[kPlayer2] = theScore[kPlayer2] + 1;
  113.                     break;
  114.             }
  115.         }
  116.     }
  117. }
  118.  
  119.  
  120. /*** getPlayerRect
  121. *
  122. * Gets rectangle in which to draw player's score
  123. */
  124. void cScorePane::getPlayerRect(Boolean thePlayer, Rect *r)
  125. {
  126.     SetRect(r, kScorePlace, kHalfSpacing, 2*kScorePlace, kScorePlace);
  127.     if (thePlayer == kPlayer2)
  128.         OffsetRect(r, kScoreSpacing, 0);
  129. }
  130.  
  131.  
  132. /*** invalScore
  133. *
  134. * Invalidate player score rectangle so it will redraw on update
  135. */
  136. void cScorePane::invalScore(Boolean thePlayer)
  137. {
  138.     Rect    r;
  139.     
  140.         /* Get rectangle where player score is displayed */
  141.     getPlayerRect(thePlayer, &r);
  142.  
  143.         /* Set to score pane's port before invalidation 
  144.         ** otherwise may be drawing in a different port */
  145.     Prepare();
  146.     InvalRect(&r);
  147. }
  148.  
  149.  
  150. /*** setTurn
  151. *
  152. * Highlight current player's turn.
  153. * Actually draw on screen since it is easy to update turn indicator.
  154. */
  155. void cScorePane::setTurn(Boolean thePlayer)
  156. {
  157.     Rect    r;
  158.     
  159.     if (thePlayer != fPlayerHilite) {
  160.         Prepare();    /* Sets up pane for drawing */
  161.         
  162.             /* Unhighlight currently highlighted player score */
  163.         getPlayerRect(fPlayerHilite, &r);
  164.         InvertRect(&r);
  165.             /* Highlight other player score */
  166.         getPlayerRect(thePlayer, &r);
  167.         InvertRect(&r);
  168.         
  169.         fPlayerHilite = thePlayer;
  170.     }
  171. }